<template>
  <div>
    <h1 style="font-size: 30px;margin-bottom:10px">回收站</h1>
    <el-table :data="tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)" v-loading="loading">
      <el-table-column
        prop="userName"
        label="创建人"
        width="100px"
      ></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="recovery(scope.row)" type="text"
            >恢复到草稿箱</el-button
          >
          <el-button @click="showMessage(scope.row)" type="text" 
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total,sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-size="pageSize"
      @current-change="handleCurrentChange"
      :page-sizes="[ 4, 8, 12]"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "Recovery",
  data() {
    return {
      tableData: [],
      loading: true,
      currentPage: 1, //当前页数
      pageSize: 4, //当前页记录数
      total: 0, //总条数
    };
  },
  methods: {
    loadBlogList() {
      this.axios.get("/blog/recoveryList").then((res) => {
        this.tableData = res.data.blogList;
        this.loading = false;
		this.total = this.tableData.length;
      });
    },
    recovery(row) {
      this.axios.get(`/blog/recovery/${row.blogId}`).then((res) => {
        if (res.data.success) {
          this.$message.success(res.data.msg);
          this.$router.push("./draft");
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    showMessage(row) {
      this.$confirm("此操作将永久删除该BLog, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteBlog(row.blogId);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteBlog(id) {
      this.axios.get(`/blog/remove/${id}`).then((res) => {
        if (res.data.success) {
          this.$message.success(res.data.msg);
          this.loadBlogList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
	handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadBlogList();
  },
};
</script>

<style>
</style>
